package classmate.view;

import javax.swing.*;
import classmate.controller.ClassmateControllerListener;
import classmate.po.Room;
import classmate.po.Student;

public class StudentDialog extends JDialog {
	private static final long serialVersionUID = 1L;
	private ClassmateControllerListener classmateControllerListener;
	private JTextField idTextField, nameTextField, birthdayTextField, phoneNumberTextField, emailTextField,
			nativePlaceTextField, addressTextField, roomIdTextField;
	private JButton confirmButton;
	private JPanel studentAddPanel;
	private Student student;
	
	public StudentDialog(StudentFrame owner, String title, Student student, Room room) {
		this.classmateControllerListener = owner.getControllerListener();
		this.classmateControllerListener.setStudentDialog(this);
		this.setTitle(title);
		this.student = student;
		this.add(getStudentAddPanel());
		this.setModal(true);
		this.setSize(330, 400);
		this.setLocationRelativeTo(null);
		this.setVisible(true);
		this.validate();
	}

	public void closeDialog() {
		this.setDefaultCloseOperation(DISPOSE_ON_CLOSE);
	}

	private JPanel getStudentAddPanel() {
		studentAddPanel = new JPanel();
		if ("添加学生信息".equals(this.getTitle())) {
			idTextField = new JTextField(22);
			nameTextField = new JTextField(22);
			birthdayTextField = new JTextField(20);
			phoneNumberTextField = new JTextField(20);
			emailTextField = new JTextField(20);
			nativePlaceTextField = new JTextField(20);
			addressTextField = new JTextField(20);
			roomIdTextField = new JTextField(20);
			confirmButton = new JButton("确定添加");
			confirmButton.setActionCommand("addStudent");
		} 
		else {
			idTextField = new JTextField(String.valueOf(student.getId()), 20);
			nameTextField = new JTextField(student.getName(), 20);
			birthdayTextField = new JTextField(String.valueOf(student.getBirthday()), 20);
			phoneNumberTextField = new JTextField(student.getPhoneNumber(), 20);
			emailTextField = new JTextField(student.getEmail(), 20);
			nativePlaceTextField = new JTextField(student.getNativePlace(), 20);
			addressTextField = new JTextField(student.getAddress(), 20);
			roomIdTextField = new JTextField(String.valueOf(student.getRoom().getRoomNo()), 20);
			confirmButton = new JButton("确定修改");
			confirmButton.setActionCommand("updateStudent");
		}
		confirmButton.addActionListener(classmateControllerListener);
		studentAddPanel.add(new JLabel("学号:"));
		studentAddPanel.add(idTextField);
		studentAddPanel.add(new JLabel("姓名:"));
		studentAddPanel.add(nameTextField);
		studentAddPanel.add(new JLabel("出生日期:"));
		studentAddPanel.add(birthdayTextField);
		studentAddPanel.add(new JLabel("手机号码:"));
		studentAddPanel.add(phoneNumberTextField);
		studentAddPanel.add(new JLabel("电子邮箱:"));
		studentAddPanel.add(emailTextField);
		studentAddPanel.add(new JLabel("籍贯:"));
		studentAddPanel.add(nativePlaceTextField);
		studentAddPanel.add(new JLabel("家庭地址:"));
		studentAddPanel.add(addressTextField);
		studentAddPanel.add(new JLabel("宿舍:"));
		studentAddPanel.add(roomIdTextField);
		studentAddPanel.add(confirmButton);
		return studentAddPanel;
	}

	public JTextField getIdTextField() {
		return idTextField;
	}

	public JTextField getNameTextField() {
		return nameTextField;
	}

	public JTextField getBirthdayTextField() {
		return birthdayTextField;
	}

	public JTextField getPhoneNumberTextField() {
		return phoneNumberTextField;
	}

	public JTextField getEmailTextField() {
		return emailTextField;
	}

	public JTextField getNativePlaceTextField() {
		return nativePlaceTextField;
	}

	public JTextField getAddressTextField() {
		return addressTextField;
	}

	public JTextField getRoomIdTextField() {
		return roomIdTextField;
	}

	public JButton getConfirmButton() {
		return confirmButton;
	}

}
